package com.jingdianjichi.wx.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class WxChatMsgFactory implements InitializingBean {
    @Resource
    private List<WxChatMsgHandler> wxChatMsgHandlerList;
    private Map<WxChatMsgTypeEnum,WxChatMsgHandler> handlerMap=new HashMap<>();
    public WxChatMsgHandler getHandlerByMsgType(String msgType){
        log.info("handlerMap:{}",handlerMap);
        WxChatMsgTypeEnum byMsgType = WxChatMsgTypeEnum.getByMsgType(msgType);
        WxChatMsgHandler wxChatMsgHandler=handlerMap.get(byMsgType);
        return wxChatMsgHandler;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for(WxChatMsgHandler wxChatMsgHandler:wxChatMsgHandlerList){
            handlerMap.put(wxChatMsgHandler.getMsgType(),wxChatMsgHandler);
        }
    }
}
